package com.neusoft.service.impl;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.mapper.BusinessMapper;
import com.neusoft.po.Business;
import com.neusoft.po.BusinessGet;
import com.neusoft.service.BusinessService;
import com.neusoft.util.MD5Lock;

@Service
public class BusinessServiceImpl implements BusinessService {

	@Autowired
	private BusinessMapper businessMapper;

	@Override
	@Transactional
	public List<BusinessGet> listBusinessByOrderTypeId(Integer orderTypeId) {
		List<Business> businesses = businessMapper.listBusinessByOrderTypeId(orderTypeId);
		List<BusinessGet> getList = new ArrayList<BusinessGet>();
		for (Business b : businesses) {
			BusinessGet get = new BusinessGet(b);// 封装数据
			getList.add(get);
		}
		return getList;
	}

	@Override
	@Transactional
	public BusinessGet getBusinessById(Integer businessId) {
		Business business = businessMapper.getBusinessById(businessId);
		BusinessGet get = new BusinessGet(business);// 封装数据
		return get;
	}

	@Override
	public int saveBusiness(Business business) {
		// TODO Auto-generated method stub
		try {
			business.setBusinessPassword(MD5Lock.getMD5Str(business.getBusinessPassword()));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return businessMapper.saveBusiness(business);
	}

	@Override
	public int updateBusiness(Business business) {
		// TODO Auto-generated method stub
		try {
			business.setBusinessPassword(MD5Lock.getMD5Str(business.getBusinessPassword()));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//if(!Lock.BusinessLock(business)) {
		//	return 0;
		//}
		return businessMapper.updateBusiness(business);
	}

	@Override
	public int removeBusiness(Integer businessId) {
		// TODO Auto-generated method stub
		return businessMapper.removeBusiness(businessId);
	}

	@Override
	public BusinessGet getBusinessByIdByPass(Business business) {
		// TODO Auto-generated method stub
		try {
			business.setBusinessPassword(MD5Lock.getMD5Str(business.getBusinessPassword()));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Business temp = businessMapper.getBusinessByIdByPass(business);
		if (temp == null) {
			return null;
		}
		BusinessGet get = new BusinessGet(temp);
		return get;
	}

	@Override
	public int getNewIds() {
		// TODO Auto-generated method stub
		return businessMapper.getNewIds();
	}
}
